package com.stone;

public class BinaryTree {

    public static void main(String[] args) {

        final int[] values = { 1, 3, 4, 5, 2, 8, 6, 7, 9, 0 };

        BinaryTree binaryTree = new BinaryTree(values);

        System.out.println(binaryTree);
    }


    TreeNode root;

    static class TreeNode {
        int value;
        TreeNode left;
        TreeNode right;

        public TreeNode(int paraValue) {
            this.value = paraValue;
        }

        public TreeNode() {

        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "value=" + value +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    public BinaryTree(int[] array) {
        root = createBinaryTreeByArray(array, 0);
    }

    private TreeNode createBinaryTreeByArray(int[] array, int index) {
        TreeNode tn = null;
        if (index < array.length) {
            int value = array[index];
            tn = new TreeNode(value);
            tn.left = createBinaryTreeByArray(array, 2 * index + 1);
            tn.right = createBinaryTreeByArray(array, 2 * index + 2);
            return tn;
        }
        return tn;
    }
}